package org.velocity;

import net.e175.klaus.solarpositioning.AzimuthZenithAngle;
import net.e175.klaus.solarpositioning.DeltaT;
import net.e175.klaus.solarpositioning.SPA;
import net.e175.klaus.solarpositioning.SunriseTransitSet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

/**
 * ClassName: ${NAME}
 * Package: org.example
 * Description:
 *
 * @Author BuTang
 * @Create ${DATE} ${TIME}
 * @Version 1.0
 */
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }

    @Test
    public void test(){
        LocalDateTime dateTime = LocalDateTime.of(2023, 11, 20, 10, 30, 0);
        double deltaT = DeltaT.estimate(LocalDate.from(dateTime));
        ZoneId zoneId = ZoneId.of("Asia/Shanghai");
        ZonedDateTime zonedDateTime = ZonedDateTime.of(dateTime,zoneId);
        double latitude = 30.23800;
        double longitude = 120.13431;

        SunriseTransitSet sunriseTransitSet = SPA.calculateSunriseTransitSet(zonedDateTime, latitude, longitude, deltaT);
        ZonedDateTime sunrise = sunriseTransitSet.getSunrise();
        ZonedDateTime sunset = sunriseTransitSet.getSunset();

        sunrise.toLocalDate();
        System.out.println(sunrise);
        System.out.println(sunset);
    }


    @Test
    public void test1() throws Exception {
        long start = System.currentTimeMillis();

        //计算太阳高度角的参数，从上到下分别为纬度，经度，海拔高度，大气压力，温度，时区（zoneIdName，zoneId）
//        double latitude = 30.23800;
        double latitude = 39.54;
//        double longitude = 120.13431;
        double longitude = 116.25;
        double elevation = 10;
        double pressure = 1000;
        double temperature = 5;
        String zoneIdName = "Asia/Shanghai";
        ZoneId zoneId = ZoneId.of("Asia/Shanghai");

        String filePath = "C:\\Users\\11921\\Desktop\\Java-Working\\my_project\\AngularVelocity\\src\\main\\java\\org\\example\\velocity.xlsx";
        FileInputStream fileInputStream = new FileInputStream(filePath) ;
        Workbook workbook = new XSSFWorkbook(fileInputStream);
        Sheet sheet = workbook.getSheet("sheet1");


        //创建需要计算太阳高度角的时间节点
        LocalDateTime dateTime = LocalDateTime.of(2023, 11, 20, 10, 30, 0);

        //开始计算，获取结果result
        AzimuthZenithAngle result =SpaUtils.getAzimuthAndZenithAngle(dateTime, zoneId, latitude, longitude, elevation, pressure, temperature);

        //高度角
        double eleAngle = 90 - result.getZenithAngle();
        System.out.println("此时太阳高度角为："+eleAngle+"°");
        //方位角
        double aziumthAngel = result.getAzimuth();
        System.out.println("此时太阳方位角为："+aziumthAngel+"°");
        System.out.println("计算 "+ dateTime+ " 的太阳位置总耗时为 " +(System.currentTimeMillis()-start) +"ms");


        LocalDateTime oldTime = dateTime ;
        LocalDateTime nowTime = dateTime ;

        //高度角
        double oldAngle1 ;
        double nowAngle1 = 0;
        //方位角
        double oldAngle2 ;
        double nowAngle2 = 0;
        //高度角
        double anteredAngle1 ;
        //方位角
        double anteredAngle2 ;
        double num = 1;

        //用于高度角的累计
        double addNumber1 = 0 ;
        double addNumber2 = 0 ;

        oldAngle1 = 90 - SpaUtils.getAzimuthAndZenithAngle(oldTime, zoneId, latitude, longitude, elevation, pressure, temperature).getZenithAngle();

        oldAngle2 = SpaUtils.getAzimuthAndZenithAngle(oldTime, zoneId, latitude, longitude, elevation, pressure, temperature).getAzimuth();

        for (num = 1; num < 86400 ; num++) {

            //获取行
            Row row = sheet.createRow((int) num);


            nowTime = nowTime.plusSeconds(1);

            //高度角有关的计算
            nowAngle1 = 90 - SpaUtils.getAzimuthAndZenithAngle(nowTime, zoneId, latitude, longitude, elevation, pressure, temperature).getZenithAngle();
            anteredAngle1 = Math.abs( Math.abs(nowAngle1)  - Math.abs(oldAngle1) )  ;
            row.createCell(1).setCellValue(anteredAngle1);
            addNumber1 += anteredAngle1;

            //方位角 角有关的计算
            nowAngle2 = SpaUtils.getAzimuthAndZenithAngle(nowTime, zoneId, latitude, longitude, elevation, pressure, temperature).getAzimuth();
            anteredAngle2 = Math.abs( Math.abs(nowAngle2)  - Math.abs(oldAngle2) ) ;
            row.createCell(2).setCellValue(anteredAngle2);
            addNumber2 += anteredAngle2;

//            System.out.println("此时的高度角角速度为："+ anteredAngle1 + " 此时的方位角角速度为：" + anteredAngle2);
//            System.out.println(anteredAngle2);

            oldAngle1 = nowAngle1;
            oldAngle2 = nowAngle2;


//            double v = Math.sqrt(Math.pow(anteredAngle1,2)+Math.pow(anteredAngle2,2));
//            System.out.println(v);

        }

                    // 保存文件
                  FileOutputStream outputStream = new FileOutputStream(filePath) ;
                        workbook.write(outputStream);
                        System.out.println("Excel文件已成功修改！");


        double v = Math.sqrt(Math.pow(addNumber1,2)+Math.pow(addNumber2,2));
        System.out.println(v);

    }

}